package waal.pc.frontend.tiles;

import java.util.HashMap;
import java.util.Map;

import javax.swing.DefaultComboBoxModel;

import waal.pc.backend.Log;

public class TileHandler {

	private Map<String, ChannelTile> channelTiles;
	
	private DefaultComboBoxModel<String> channelDigitalTiles;
	private DefaultComboBoxModel<String> channelDigitalInputTiles;
	private DefaultComboBoxModel<String> channelDigitalOutputTiles;
	
	private DefaultComboBoxModel<String> channelAnalogTiles;
	private DefaultComboBoxModel<String> channelAnalogInputTiles;
	private DefaultComboBoxModel<String> channelAnalogOutputTiles;
	
	private Map<String, ModuleTile> moduleTiles;
	private DefaultComboBoxModel<String> moduleDigitalTiles;
	private DefaultComboBoxModel<String> moduleAnalogTiles;
	
	private DefaultComboBoxModel<String> emtyModel;
	
	public TileHandler() {
		this.channelTiles = new HashMap<String, ChannelTile>();
		
		this.channelDigitalTiles = new DefaultComboBoxModel<String>();
		this.channelDigitalInputTiles = new DefaultComboBoxModel<String>();
		this.channelDigitalOutputTiles = new DefaultComboBoxModel<String>();
		
		this.channelAnalogTiles = new DefaultComboBoxModel<String>();
		this.channelAnalogInputTiles = new DefaultComboBoxModel<String>();
		this.channelAnalogOutputTiles = new DefaultComboBoxModel<String>();
		
		this.moduleTiles = new HashMap<String, ModuleTile>();
		this.moduleDigitalTiles = new DefaultComboBoxModel<String>();
		this.moduleAnalogTiles = new DefaultComboBoxModel<String>();
		
		this.emtyModel  = new DefaultComboBoxModel<String>();
		this.makeTiles();
	}
	
	public void makeTiles() {
		this.addTile(new MeterTile());
		this.addTile(new DigitalIOTile());
		this.addTile(new AnalogOutputTile());
		this.addTile(new LinePlotTile());
	}
	
	private void addTile(Tile tile) {
		String name = tile.toString();
		Log.addToDebug(3, "TileHandler", "addTile", "add new Tile: " + name);
		if(tile.moduleTile()) {
			
			ModuleTile t = (ModuleTile) tile;
			this.moduleTiles.put(name, t);
			if(t.isDigital()){
				this.moduleDigitalTiles.addElement(name);
				Log.addToDebug(3, "TileHandler", "addTile", name + " is digital module Tile");
			} else {
				Log.addToDebug(3, "TileHandler", "addTile", name + " is analog module Tile");
				this.moduleAnalogTiles.addElement(name);
			}
		} else {
			
			ChannelTile t = (ChannelTile) tile;
			this.channelTiles.put(name, t);
			if(t.isDigital()) {
				Log.addToDebug(3, "TileHandler", "addTile", name + " is digital channel Tile");
				this.channelDigitalTiles.addElement(name);
				if(t.isInput()) {
					this.channelDigitalInputTiles.addElement(name);
					Log.addToDebug(3, "TileHandler", "addTile", name + " is digital input channel Tile");
				}
				if(t.isOutput()) {
					Log.addToDebug(3, "TileHandler", "addTile", name + " is digital output channel Tile");
					this.channelDigitalInputTiles.addElement(name);
				}
				Log.addToDebug(3, "TileHandler", "addTile", "Module: " + this.moduleTiles.size() + " digital: " + this.moduleDigitalTiles.getSize() + " analog: " + this.moduleAnalogTiles.getSize());		
			} else {
				Log.addToDebug(3, "TileHandler", "addTile", name + " is analog channel Tile");
				this.channelAnalogTiles.addElement(name);
				if(t.isInput()) {
					Log.addToDebug(3, "TileHandler", "addTile", name + " is analog input channel Tile");
					this.channelAnalogInputTiles.addElement(name);
				}
				if(t.isOutput()) {
					Log.addToDebug(3, "TileHandler", "addTile", name + " is analog output channel Tile");
					this.channelAnalogOutputTiles.addElement(name);
				}
			}
			Log.addToDebug(3, "TileHandler", "addTile", "channel: " + this.channelTiles.size() + " analog: " + this.channelAnalogTiles.getSize() + " analog input: " + this.channelAnalogInputTiles.getSize() + " analog output: " + this.channelAnalogOutputTiles.getSize());
			Log.addToDebug(3, "TileHandler", "addTile", "channel: " + this.channelTiles.size() + " digital: " + this.channelDigitalTiles.getSize() + " digital input: " + this.channelDigitalInputTiles.getSize() + " digital output: " + this.channelDigitalOutputTiles.getSize());
		}
	}
	
	
	
	
	public DefaultComboBoxModel<String> getTileList(boolean digital, boolean module, boolean input, boolean output) {
		//Log.addToDebug(3, "TileHandler", "getTileList", "Frage nach BoxModel digital: " + digital + " module: " + module + " input: " + input + " output: " +  output);
		if(module) {
			if(digital) {
				Log.addToDebug(3, "TileHandler", "getTileList", "gebe digital Module"); 
				return this.moduleDigitalTiles;
			} else {
				Log.addToDebug(3, "TileHandler", "getTileList", "gebe analog Module");
				return this.moduleAnalogTiles;
			}
		} else {
			if(digital) {
				if(input && output) {
					Log.addToDebug(3, "TileHandler", "getTileList", "gebe digital Channel");
					return this.channelDigitalTiles;
				} else if(input) {
					Log.addToDebug(3, "TileHandler", "getTileList", "gebe digital input Channel");
					return this.channelDigitalInputTiles;
				} else if(output) {
					Log.addToDebug(3, "TileHandler", "getTileList", "gebe digital outputChannel");
					return this.channelDigitalOutputTiles;
				} else {
					Log.addToDebug(3, "TileHandler", "getTileList", "gebe nix");
					return this.emtyModel;
				}
			} else {
				if(input && output) {
					Log.addToDebug(3, "TileHandler", "getTileList", "gebe analog Channel");
					return this.channelAnalogTiles;
				} else if(input) {
					Log.addToDebug(3, "TileHandler", "getTileList", "gebe analog input Channel");
					return this.channelAnalogInputTiles;
				} else if(output){
					Log.addToDebug(3, "TileHandler", "getTileList", "gebe analog output Channel");
					return this.channelAnalogOutputTiles;
				} else {
					Log.addToDebug(3, "TileHandler", "getTileList", "gebe nix");
					return this.emtyModel;
				}
			}
		}
	}
	
	public Tile getTile(boolean module, String name) {
		if(module) {
			return this.moduleTiles.get(name).clone();
		} else {
			return this.channelTiles.get(name).clone();
		}
	}
}
